eststo clear

if("`c(os)'"=="Windows"){
	local tableSaveDir = "Tables\"
	local plotSaveDir = "Plots\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "Tables/"
	local plotSaveDir = "Plots/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
//Spec1 controls are the same for both registrations and fuel use 
local spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"

local spec1LinearControls = "c.normYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"

//Other controls are not the same for both registrations and gas use 
//Registration Controls
local spec2RegControls = "`spec1Controls' i.transactionDataDummy"
local spec3RegControls = "`spec2RegControls' i.regDummyExtSpec3"
local spec4RegControls = "`spec2RegControls' i.regDummyExtSpec4"

local spec2RegLinearControls = "`spec1LinearControls' i.transactionDataDummy"
local spec3RegLinearControls = "`spec2RegLinearControls' i.regDummyExtSpec3"
local spec4RegLinearControls = "`spec2RegLinearControls' i.regDummyExtSpec4"

di "`spec3RegControls'"

//Gas use Controls
local spec2GasUseControls = "`spec1Controls'"
local spec3GasUseControls = "`spec2GasUseControls' i.gasUseDummyExtSpec3"
local spec4GasUseControls = "`spec2GasUseControls' i.gasUseDummyExtSpec4"

local spec2GasUseLinearControls = "`spec1LinearControls'"
local spec3GasUseLinearControls = "`spec2GasUseLinearControls' i.gasUseDummyExtSpec3"
local spec4GasUseLinearControls = "`spec2GasUseLinearControls' i.gasUseDummyExtSpec4"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Establish primary null
local primaryNull = 11
local altNull = round(9.7, .1)
local altNull2 = 1
local altNull3 = -4.8
local altNull4 = -6.1

local treatmentVar = "1.nosafetyind"

local fsOut = "logregistrations"
local rfOut = "loghighwaygasuse"

local fsOutPerCapita = "logRegPerCapita"
local rfOutPerCapita = "logGasUsePerCapita"

local fsControlsSpec1 = "`spec1Controls'"
local rfControlsSpec1 = "`spec1Controls'"

local fsControlsSpec1Linear = "`spec1LinearControls'"
local rfControlsSpec1Linear = "`spec1LinearControls'"

local treatControl = "nosafetyind"

local modelTitleSpec1 = "Spec1GasUse"
local modelTitleSpec1PerCapita = "Spec1GasUsePerCapita"
local modelTitleSpec1Linear = "Spec1GasUseLinear"
local nullLowSpec1 = -3.6
local nullDeltaSpec1 = 0.001
local nullHighSpec1 = 1


runModelFWL `fsOut', `rfOut', `fsControlsSpec1', `rfControlsSpec1', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec1', `nullLowSpec1', `nullDeltaSpec1', `nullHighSpec1', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOut', `rfOut', `fsControlsSpec1Linear', `rfControlsSpec1Linear', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec1Linear', `nullLowSpec1', `nullDeltaSpec1', `nullHighSpec1', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOutPerCapita', `rfOutPerCapita', `fsControlsSpec1', `rfControlsSpec1', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec1PerCapita', `nullLowSpec1', `nullDeltaSpec1', `nullHighSpec1', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsControlsSpec2 = "`spec2RegControls'"
local rfControlsSpec2 = "`spec2GasUseControls'"

local fsControlsSpec2Linear = "`spec2RegLinearControls'"
local rfControlsSpec2Linear = "`spec2GasUseLinearControls'"

local treatControl = "nosafetyind"

local modelTitleSpec2 = "Spec2GasUse"
local modelTitleSpec2PerCapita = "Spec2GasUsePerCapita"
local modelTitleSpec2Linear = "Spec2GasUseLinear"
local nullLowSpec2 = -4
local nullDeltaSpec2 = 0.001
local nullHighSpec2 = 1

runModelFWL `fsOut', `rfOut', `fsControlsSpec2', `rfControlsSpec2', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOut', `rfOut', `fsControlsSpec2Linear', `rfControlsSpec2Linear', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec2Linear', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOutPerCapita', `rfOutPerCapita', `fsControlsSpec2', `rfControlsSpec2', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec2PerCapita', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsControlsSpec3 = "`spec3RegControls'"
local rfControlsSpec3 = "`spec3GasUseControls'"

local fsControlsSpec3Linear = "`spec3RegLinearControls'"
local rfControlsSpec3Linear = "`spec3GasUseLinearControls'"

local treatControl = "nosafetyind"

local modelTitleSpec3 = "Spec3GasUse"
local modelTitleSpec3PerCapita = "Spec3GasUsePerCapita"
local modelTitleSpec3Linear = "Spec3GasUseLinear"
local nullLowSpec3 = -8
local nullDeltaSpec3 = 0.001
local nullHighSpec3 = 5

runModelFWL `fsOut', `rfOut', `fsControlsSpec3', `rfControlsSpec3', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec3', `nullLowSpec3', `nullDeltaSpec3', `nullHighSpec3', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOut', `rfOut', `fsControlsSpec3Linear', `rfControlsSpec3Linear', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec3Linear', `nullLowSpec3', `nullDeltaSpec3', `nullHighSpec3', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOutPerCapita', `rfOutPerCapita', `fsControlsSpec3', `rfControlsSpec3', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec3PerCapita', `nullLowSpec3', `nullDeltaSpec3', `nullHighSpec3', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsControlsSpec4 = "`spec4RegControls'"
local rfControlsSpec4 = "`spec4GasUseControls'"

local fsControlsSpec4Linear = "`spec4RegLinearControls'"
local rfControlsSpec4Linear = "`spec4GasUseLinearControls'"

local treatControl = "nosafetyind"

local modelTitleSpec4 = "Spec4GasUse"
local modelTitleSpec4PerCapita = "Spec4GasUsePerCapita"
local modelTitleSpec4Linear = "Spec4GasUseLinear"
local nullLowSpec4 = -10
local nullDeltaSpec4 = 0.001
local nullHighSpec4 = 10

runModelFWL `fsOut', `rfOut', `fsControlsSpec4', `rfControlsSpec4', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec4', `nullLowSpec4', `nullDeltaSpec4', `nullHighSpec4', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOut', `rfOut', `fsControlsSpec4Linear', `rfControlsSpec4Linear', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec4Linear', `nullLowSpec4', `nullDeltaSpec4', `nullHighSpec4', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOutPerCapita', `rfOutPerCapita', `fsControlsSpec4', `rfControlsSpec4', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec4PerCapita', `nullLowSpec4', `nullDeltaSpec4', `nullHighSpec4', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

//Combine all of the IV information
python script "`pythonSaveDir'combineIV.py"

//Output tables
constructTable GasUsePerCapitaiv; Fleet Gas Use Per Capita; Spec1GasUsePerCapita Spec2GasUsePerCapita Spec3GasUsePerCapita Spec4GasUsePerCapita; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
constructTable GasUseiv; Fleet Gas Use; Spec1GasUse Spec2GasUse Spec3GasUse Spec4GasUse; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
constructTable GasUseivShort; Fleet Gas Use; Spec1GasUse Spec2GasUse; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
